<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
			<uni-forms-item name="title" label="商品名" required>
				<uni-easyinput placeholder="要回收的商品名" v-model="formData.title" disabled="true"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="user_id" label="出售方">
				<uni-easyinput :placeholder="formData.address_id[0].name" disabled="true"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="address_id" label="订单地址" required>
				<uni-easyinput v-module="formData.address_id"
					:placeholder="formData.address_id[0].region+''+formData.address_id[0].address"
					disabled="true"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="date_time" label="上门时间" required>
				<uni-easyinput placeholder="客户要求多久去上门取件" v-model="formData.date_time" disabled></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="number" label="物品数量" required>
				<uni-easyinput type="number" v-model="formData.number" disabled="true"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="content" label="物品情况描述" required>
				<uni-easyinput v-model="formData.content" disabled="true"></uni-easyinput>
			</uni-forms-item>

			<uni-forms-item name="cancel_desc" label="取消原因">
				<uni-easyinput v-model="formData.cancel_desc" disabled="true"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="create_time" label="订单创建时间">
				<uni-datetime-picker return-type="timestamp" v-model="formData.create_time"
					disabled="true"></uni-datetime-picker>
			</uni-forms-item>
			<uni-forms-item name="status" label="订单状态" required>
				<uni-tag :text="orderStatus(formData.status).name" :type="orderStatus(formData.status).color" />
			</uni-forms-item>
			<uni-forms-item name="price" label="回收金额" required>
				<uni-easyinput type="number" v-model="formData.price"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item v-if="formData.status==1" name="is_handle" label="是否处理" required>
				<switch @change="binddata('is_handle', $event.detail.value)" :checked="formData.is_handle"></switch>
			</uni-forms-item>
			<uni-forms-item v-else-if="formData.status==3" name="is_handle" label="是否取消" required>
				<switch @change="binddata('is_handle', $event.detail.value)" :checked="formData.is_handle"></switch>
			</uni-forms-item>
			<uni-forms-item v-if="formData.status!=0" name="handle_time" label="订单完成时间" required>
				<uni-datetime-picker return-type="timestamp" v-model="formData.handle_time"
					:disabled="formData.status==2 || formData.status==4"></uni-datetime-picker>
			</uni-forms-item>

			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/uni-id-base-order.js';
	import {
		formatDate
	} from '../../uni_modules/uni-dateformat/components/uni-dateformat/date-format.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'uni-id-base-order,uni-id-address';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"title": "",
				"goods_id": "",
				"address_id": null,
				"is_handle": false,
				"status": 0,
				"price": null,
				"date_time": "",
				"number": null,
				"content": "",
				"imgs": [],
				"handle_time": null,
				"cancel_desc": null,
				"user_id": null,
				"create_time": null
			}
			return {
				formData,
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onLoad(e) {
			if (e.id) {
				const id = e.id
				this.formDataId = id
				this.getDetail(id)
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			orderStatus(status) {
				switch (status) {
					case 0:
						return {
							'name': '待确认',
							'color': 'primary'
						}
						break;
					case 1:
						return {
							'name': '待收款',
							'color': 'warning'
						}
						break;
					case 2:
						return {
							'name': '已收款',
							'color': 'success'
						}
						break;
					case 3:
						return {
							'name': '取消中',
							'color': 'warning'
						}
						break;
					case 4:
						return {
							'name': '已取消',
							'color': 'info'
						}
						break;
					default:
						break;
				}
			},
			/**
			 * 验证表单并提交
			 */
			submit() {
				if (this.formData.handle_time == null && this.formData.status != 0) {
					uni.showToast({
						title: '请选则完成时间',
						duration: 800,
						icon: "error"
					});
					return
				} else if (this.formData.price == null) {
					uni.showToast({
						title: '请填写回收金额',
						duration: 800,
						icon: "error"
					});
					return
				}
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				value.price = parseInt(value.price)
				if (value.status == 1 && value.is_handle) {
					value.status = 2
				} else if (value.status == 1 && !value.is_handle) {
					value.status = 1
				} else if (value.status == 3 && value.is_handle) {
					value.status = 4
				} else if (value.status == 3 && !value.is_handle) {
					value.status = 3
				}
				// 使用 clientDB 提交数据
				return db.collection('uni-id-base-order').doc(this.formDataId).update({
					handle_time: value.handle_time,
					price: value.price,
					status: value.status,
					is_handle: value.is_handle
				}).then((res) => {
					uni.showToast({
						title: '修改成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},

			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).doc(id).field(
					"title,goods_id,address_id.region,address_id.address,address_id.name,is_handle,status,price,date_time,number,content,imgs,handle_time,cancel_desc,user_id,create_time"
				).get().then((res) => {
					const data = res.result.data[0]
					if (data) {
						this.formData = data

					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>